package 代码记录.指定公司代码练习记录.华为_2024.分值200;

import java.util.*;

/**
 * @author zx
 * @create 2024-02-16 14:41
 * 自己熟悉的做法
 */
public class 二叉树层次遍历_2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String strPost = sc.next();
        String strIn = sc.next();
        int[] post = new int[strPost.length()];
        int[] in = new int[strIn.length()];
        for(int i = 0;i < strPost.length();i++){
            post[i] = strPost.charAt(i) - 'A';
        }
        for(int i = 0;i < strIn.length();i++){
            in[i] = strIn.charAt(i) - 'A';
        }
        TreeNode treeNode = buildTree(in, post);//由后序遍历和中序遍历构建二叉树
        List<List<Integer>> lists = levelOrder(treeNode);//层序遍历二叉树
        List<Integer> temp = new ArrayList<>();
        for (List<Integer> list : lists) {
            for (Integer i : list) {
                temp.add(i);
            }
        }
        StringBuilder res = new StringBuilder();
        for (Integer i : temp) {
            res.append((char) (i + 'A'));
        }
        System.out.println(res.toString());
    }
    // 后序遍历的数组最后一个元素代表的即为根节点
    private static int index;
    public static TreeNode buildTree(int[] inorder, int[] postorder) {
        index = postorder.length - 1;
        return buildTreeByPostOrder(inorder,postorder,0,postorder.length - 1);

    }
    //在preOrder的[left...right]借助后序遍历还原二叉树,返回二叉树的根节点
    private static TreeNode buildTreeByPostOrder(int[] inorder, int[] postorder, int left, int right){
        if(left > right){
            return null;
        }
        TreeNode root = new TreeNode(postorder[index]);
        index--;
        int pos = findIndex(inorder,root.val);
        //必须先右后左
        root.right = buildTreeByPostOrder(inorder, postorder,pos + 1,right);
        root.left = buildTreeByPostOrder(inorder, postorder, left,pos - 1);
        return root;
    }
    //找到给定值在中序遍历中的索引
    private static int findIndex(int[]inorder,int val){
        for(int i = 0;i < inorder.length;i++){
            if(val == inorder[i]){
                return i;
            }
        }
        return -1;
    }
    public static List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if(root == null){
            return res;
        }
        Deque<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            List<Integer> level = new ArrayList<>();
            int levelCount = queue.size();
            for(int i = 0;i < levelCount;i++){
                TreeNode node = queue.poll();
                level.add(node.val);
                if(node.left != null){
                    queue.offer(node.left);
                }
                if(node.right != null){
                    queue.offer(node.right);
                }
            }
            res.add(level);
        }
        return res;
    }

    static class TreeNode {
        public int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int val) { this.val = val; }
    }
}
